package io.kiki.sba.registry.server.clientInterface.converter;

import io.kiki.sba.registry.common.model.ServerData;
import io.kiki.sba.registry.common.model.store.BaseInfo.ClientVersion;
import io.kiki.sba.registry.common.model.store.DataInfo;
import io.kiki.sba.registry.common.model.store.Publisher;
import io.kiki.sba.registry.common.model.store.Url;
import io.kiki.sba.registry.core.model.Data;
import io.kiki.sba.registry.core.model.PublisherRegister;

import java.util.ArrayList;
import java.util.List;


public final class PublisherConverter {
    private static final Converter<PublisherRegister, Publisher> publisherConverter = publisherRegister -> {
        Publisher publisher = new Publisher();

        fillCommonRegion(publisher, publisherRegister);
        publisher.setServerDataList(convert(publisherRegister.getDataList()));

        return publisher;
    };

    private PublisherConverter() {
    }

    public static void fillCommonRegion(Publisher publisher, PublisherRegister publisherRegister) {
        publisher.setAppName(publisherRegister.getAppName());
        // ZONE MUST BE CURRENT SESSION ZONE
        publisher.setZone(publisherRegister.getZone());
        publisher.setClientId(publisherRegister.getClientId());
        publisher.setDataId(publisherRegister.getDataId());
        publisher.setGroup(publisherRegister.getGroup());
        publisher.setInstanceId(publisherRegister.getInstanceId());
        publisher.setRegisterId(publisherRegister.getRegistId());
        publisher.setProcessId(publisherRegister.getProcessId());
        if (publisherRegister.getVersion() != null) {
            publisher.setVersion(publisherRegister.getVersion());
        }

        // registerTimestamp must happen from server,client time maybe different cause pub and
        // unPublisher fail
        publisher.setRegisterTimestamp(System.currentTimeMillis());

        publisher.setClientRegisterTimestamp(publisherRegister.getTimestamp());
        publisher.setClientUrl(new Url(publisherRegister.getIp(), publisherRegister.getPort()));
        publisher.setAttributes(publisherRegister.getAttributes());
        publisher.setClientVersion(ClientVersion.StoreData);

        DataInfo dataInfo = new DataInfo(publisherRegister.getInstanceId(), publisherRegister.getDataId(), publisherRegister.getGroup());
        publisher.setDataInfoId(dataInfo.getDataInfoId());
    }

    /**
     * PublisherRegister to Publisher
     */
    public static Publisher convert(PublisherRegister publisherRegister) {
        return publisherConverter.convert(publisherRegister);
    }

    public static List<ServerData> convert(List<Data> dataList) {
        List<ServerData> serverDataList = new ArrayList<>();
        if (null != dataList) {
            for (Data data : dataList) {
                ServerData serverData = new ServerData(ServerData.getBytes(data.getData()));
                serverDataList.add(serverData);
            }
        }
        return serverDataList;
    }
}
